#include "CardType.h"
#include "CardTypeData.h"
#include "PokerData.h"
#include <iostream>
#include "CardTypeManager.h"

Straight::Straight() {
}

Straight::~Straight() {
}

CardTypeData Straight::getCardType(const PokerData & data) const{
  vector<Poker> pokers = data.getPokers();
  int count = pokers.size();
  if (count > 4) {
    PokerData::sortData(pokers);
    int len = 1;
    int value = pokers.at(0).value;
    int current = value;
    for (int i = 1; i < count - 1; i++) {
      const Poker & p = pokers.at(i);
      if (current - 1 == p.value) {
        len++;
        current = p.value;
      } else if (current == p.value) {
        continue;
      } else {
        len = 1;
        current = value = p.value;
      }
      if (len == 5)
        break;
    }
    bool straight = false;
    if (len == 5) {
      straight = true;
    } else {
      if (pokers.at(0).value == ACE) {
        bool five = false;
        bool four = false;
        bool three = false;
        bool two = false;
        for (int i = 1; i < count; i++) {
          const Poker & p = pokers.at(i);
          if (p.value == FIVE)
            five = true;
          else if (p.value == FOUR)
            four = true;
          else if (p.value == THREE)
            three = true;
          else if (p.value == TWO)
            two = true;
        }
        if (five && four && three && two) {
          straight = true;
          value = FIVE;
        }
      }
    }
    if (straight) {
      CardTypeData cardTypeData;
      cardTypeData.type = CardType::Straight;
      cardTypeData.first = value;
      return cardTypeData;
    }
  }
  return getNextType()->getCardType(data);
}

const CardType * Straight::getNextType() const {
  return CardTypeManager::sharedCardTypeManager()->getCardType(CardType::ThreeOfAKind);
}
